package com.taoym.voucher.xcx.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.taoym.voucher.common.enums.ActivityOrderStatusEnum;
import com.taoym.voucher.common.enums.OrderIsStatisticsBillEnum;
import com.taoym.voucher.common.enums.UserWithdrawStatusEnum;
import com.taoym.voucher.common.utils.SecureUtils;
import com.taoym.voucher.common.utils.ThreadLocalUtils;
import com.taoym.voucher.common.utils.TimeUtil;
import com.taoym.voucher.xcx.domain.XcxUserInfo;
import com.taoym.voucher.xcx.entity.dto.XcxUpdateUserInfoDTO;
import com.taoym.voucher.xcx.entity.dto.XcxUserLoginDTO;
import com.taoym.voucher.xcx.entity.vo.XcxGetChiefProfitVO;
import com.taoym.voucher.xcx.entity.vo.XcxGetUserInfoDetailVO;
import com.taoym.voucher.xcx.entity.vo.XcxGetUserInfoVO;
import com.taoym.voucher.xcx.mapper.XcxActivityOrderMapper;
import com.taoym.voucher.xcx.mapper.XcxUserInfoMapper;
import com.taoym.voucher.xcx.mapper.XcxUserWithdrawMapper;
import com.taoym.voucher.xcx.service.XcxUserInfoService;
import org.springframework.stereotype.Service;
import org.springframework.util.ObjectUtils;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;

/**
 * @coder taoym
 * @datetime 2021/6/4 16:27
 * @desc 用户信息服务接口实现
 */
@Service
public class XcxUserInfoServiceImpl implements XcxUserInfoService {

    @Resource
    private XcxUserInfoMapper xcxUserInfoMapper;
    @Resource
    private XcxActivityOrderMapper xcxActivityOrderMapper;
    @Resource
    private XcxUserWithdrawMapper xcxUserWithdrawMapper;

    @Override
    public Integer userLogin(XcxUserLoginDTO data) {
        String uid = ThreadLocalUtils.get().getUid();

        XcxUserInfo xcxUserInfo = new XcxUserInfo();
        xcxUserInfo.setAvatarUrl(data.getAvatarUrl());
        xcxUserInfo.setNickName(data.getNickName());
        xcxUserInfo.setUpdateTime(LocalDateTime.now());
        xcxUserInfo.setFormal(1);

        return xcxUserInfoMapper.update(xcxUserInfo, new UpdateWrapper<XcxUserInfo>().eq("uid", uid));
    }

    @Override
    public XcxGetUserInfoDetailVO getUserInfoDetail() {
        String uid = ThreadLocalUtils.get().getUid();

        XcxUserInfo xcxUserInfo = xcxUserInfoMapper.selectOne(new QueryWrapper<XcxUserInfo>()
                .select("nick_name", "avatar_url", "zfb", "zfb_name", "email", "formal", "chief_uid")
                .eq("uid", uid));

        if (ObjectUtils.isEmpty(xcxUserInfo)) {
            return null;
        }

        XcxGetUserInfoDetailVO xcxGetUserInfoDetailVO = new XcxGetUserInfoDetailVO();

        xcxGetUserInfoDetailVO.setEmail(xcxUserInfo.getEmail());
        xcxGetUserInfoDetailVO.setAvatarUrl(xcxUserInfo.getAvatarUrl());
        xcxGetUserInfoDetailVO.setZfbName(xcxUserInfo.getZfbName());
        xcxGetUserInfoDetailVO.setZfb(SecureUtils.decrypt(xcxUserInfo.getZfb()));
        xcxGetUserInfoDetailVO.setFormal(xcxUserInfo.getFormal());
        xcxGetUserInfoDetailVO.setNickName(xcxUserInfo.getNickName());
        xcxGetUserInfoDetailVO.setChiefUid(xcxUserInfo.getChiefUid());
        xcxGetUserInfoDetailVO.setUid(uid);

        return xcxGetUserInfoDetailVO;
    }

    @Override
    public Integer updateUserInfo(XcxUpdateUserInfoDTO data) {
        String uid = ThreadLocalUtils.get().getUid();

        XcxUserInfo xcxUserInfo = new XcxUserInfo();
        xcxUserInfo.setZfb(SecureUtils.encode(data.getZfb()));
        xcxUserInfo.setZfbName(data.getZfbName());
        xcxUserInfo.setEmail(data.getEmail());
        xcxUserInfo.setUpdateTime(LocalDateTime.now());
        return xcxUserInfoMapper.update(xcxUserInfo, new UpdateWrapper<XcxUserInfo>().eq("uid", uid));
    }

    @Override
    public List<XcxGetUserInfoVO> getUserInfos() {

        String uid = ThreadLocalUtils.get().getUid();

        List<XcxUserInfo> userInfos = xcxUserInfoMapper.selectList(new QueryWrapper<XcxUserInfo>()
                .select("uid", "nick_name", "create_time")
                .eq("chief_uid", uid)
                .orderByDesc("create_time"));

        List<XcxGetUserInfoVO> xcxGetUserInfoVOS = new ArrayList<>(userInfos.size());
        userInfos.forEach(e -> {
            XcxGetUserInfoVO xcxGetUserInfoVO = new XcxGetUserInfoVO();
            xcxGetUserInfoVO.setUid(e.getUid());
            xcxGetUserInfoVO.setCreateDate(e.getCreateTime().toLocalDate());
            xcxGetUserInfoVO.setNickName(e.getNickName());
            xcxGetUserInfoVOS.add(xcxGetUserInfoVO);
        });
        return xcxGetUserInfoVOS;
    }

    @Override
    public XcxGetChiefProfitVO chiefProfit() {

        XcxGetChiefProfitVO xcxGetChiefProfitVO = new XcxGetChiefProfitVO();
        String uid = ThreadLocalUtils.get().getUid();

        // 总收益 查询用户的提现总金额
        String allWithdraw = xcxUserWithdrawMapper.withdraw(uid, UserWithdrawStatusEnum.SUCCESS.getCode());
        xcxGetChiefProfitVO.setAllWithdraw(allWithdraw);
        // 在路上收益
        String onLoadFee = xcxUserWithdrawMapper.withdraw(uid, UserWithdrawStatusEnum.IS_APPLY.getCode());
        xcxGetChiefProfitVO.setOnLoadFee(onLoadFee);
        // 可提现团队佣金
        LocalDateTime prevMonthEnd = TimeUtil.getMonthEndTime(LocalDate.now().plusMonths(-1));
        String chiefFee = xcxActivityOrderMapper.chiefFee(uid, ActivityOrderStatusEnum.IS_SETTLEMENT.getCode(), OrderIsStatisticsBillEnum.NO.getCode(), prevMonthEnd);
        xcxGetChiefProfitVO.setChiefFee(chiefFee);
        // 可提现自购佣金
        String orderUserFee = xcxActivityOrderMapper.orderUserFee(uid, ActivityOrderStatusEnum.IS_SETTLEMENT.getCode(), OrderIsStatisticsBillEnum.NO.getCode(), prevMonthEnd);
        xcxGetChiefProfitVO.setOrderUserFee(orderUserFee);

        String canWithdraw = new BigDecimal(ObjectUtils.isEmpty(chiefFee) ? "0.00" : chiefFee)
                .add(new BigDecimal(ObjectUtils.isEmpty(orderUserFee) ? "0.00" : orderUserFee)).toString();
        xcxGetChiefProfitVO.setCanWithdrawProfit(canWithdraw);

        int day = LocalDate.now().getDayOfMonth();
        xcxGetChiefProfitVO.setCanWithdraw(day > 25 && day < 30);

        return xcxGetChiefProfitVO;
    }
}
